//
//  SleepRateView.swift
//  MoyoungHome
//
//  Created by 李然 on 2022/9/27.
//

import UIKit
import MYModule

import RxSwift
import RxCocoa


class SleepRatioCell: SleepBaseCell {
    
    let restfulView = SleepSubRaTioView()
    let lightView = SleepSubRaTioView()
    let rapidEyemovementView = SleepSubRaTioView()
    
    
    override func makeUI() {
        super.makeUI()
        
        self.backgroundColor = UIColor.background
        
        // 控制圆大小
        pieChartView.setExtraOffsets(left: -18, top: -18, right: -18, bottom: -18)
        
        restfulView.titleLabel.text = "sleep_restful".language
        restfulView.icon.backgroundColor = UIColor.Sleep.restful
        
        lightView.titleLabel.text = "sleep_light".language
        lightView.icon.backgroundColor = UIColor.Sleep.light
        
        rapidEyemovementView.titleLabel.text = "sleep_eye_movement".language
        rapidEyemovementView.icon.backgroundColor = UIColor.Sleep.rem
        
        bgView.addSubview(titleLabel)
        bgView.addSubview(pieChartView)
        bgView.addSubview(restfulView)
        bgView.addSubview(lightView)
        bgView.addSubview(rapidEyemovementView)
        
        titleLabel.snp.makeConstraints { make in
            make.leading.equalTo(26)
            make.top.equalTo(20)
        }

        pieChartView.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(39)
            make.leading.equalToSuperview().offset(50)
            make.size.equalTo(120)
            make.bottom.equalToSuperview().offset(-35)
        }
        
        restfulView.snp.makeConstraints { make in
            make.bottom.equalTo(lightView.snp.top).offset(-20)
            make.left.equalTo(lightView.snp.left)
        }
        
        lightView.snp.makeConstraints { make in
            make.centerY.equalTo(pieChartView.snp.centerY)
            make.left.equalTo(pieChartView.snp.right).offset(40)
        }
        
        rapidEyemovementView.snp.makeConstraints { make in
            make.top.equalTo(lightView.snp.bottom).offset(20)
            make.left.equalTo(lightView.snp.left)
        }
        
 
        let totalStack = StackView()
        totalStack.axis = .vertical
        totalStack.spacing = 10
        totalStack.alignment = .center
        totalStack.addArrangedSubview(totalView)
        
        let totalTipLabel = Label()
        totalTipLabel.textColor = UIColor.assist_222222
        totalTipLabel.font = Fonts.systemFont(ofSize: 12)
        totalTipLabel.text = "sleep_total".language
        totalStack.addArrangedSubview(totalTipLabel)
        
        bgView.addSubview(totalStack)
        totalStack.snp.makeConstraints { make in
            make.center.equalTo(pieChartView)
        }
        
        
    }
    
    override func bind(to cellType: SleepViewModel.CellType) {
        super.bind(to: cellType)
        
        guard let recordDriver = cellType.viewModel as? BehaviorRelay<SleepModel> else { return }
        
        let disposeBag = DisposeBag()
    
        recordDriver.subscribe(onNext:{ [weak self] in
            
            self?.setData($0)
            
        }).disposed(by: disposeBag)

    }
    
    
    func setData(_ record:SleepModel){
        
       
        let light = record.light
        let deep = record.deep
        let total = record.total
        
        let fast = total - light - deep
        
        let totalHours = total/60
        let totalMinutes = total - totalHours * 60
        
        let totalTuple = timeString(hour: totalHours, minute: totalMinutes)
        totalHourLabel.text = totalTuple.hour
        totalMinuteLabel.text = totalTuple.minute
        
        
        let deepTimes = timeOf(time: deep)
        let deepTuple = timeString(hour: deepTimes.hour, minute: deepTimes.minute)
        restfulView.hourLabel.text = deepTuple.hour
        restfulView.minLabel.text = deepTuple.minute
        
        let lightTimes = timeOf(time: light)
        let lightTuple = timeString(hour: lightTimes.hour, minute: lightTimes.minute)
        lightView.hourLabel.text = lightTuple.hour
        lightView.minLabel.text = lightTuple.minute
        
        let rapidEyeMovement = timeOf(time: fast)
        let remTuple = timeString(hour: rapidEyeMovement.hour, minute: rapidEyeMovement.minute)
        rapidEyemovementView.hourLabel.text = remTuple.hour
        rapidEyemovementView.minLabel.text = remTuple.minute
        
        var colors = [UIColor.Sleep.restful,
                      UIColor.Sleep.light,
                      UIColor.Sleep.rem]
        
        var data = [Double(deep), Double(light), Double(fast)]
        
        if record.isEmpty {
            colors = [UIColor.assist_EEEEEE]
            data = [100]
        }
        
        self.pieChartView.setData(data, colors: colors)
        
    }

    private func timeOf(time: Int, hour_unit: String = "sleep_hour".language, minutes_unit: String = "sleep_min".language) -> (hour: Int, minute: Int) {
        let hours = time/60
        let minutes = time - hours * 60
        return (hours, minutes)
    }
    
    func timeString(hour: Int, minute: Int) ->(hour: String, minute: String) {
        if hour == 0 && minute == 0 {
            return ("--", "--")
        }
        return ("\(hour.leadingZero)", "\(minute.leadingZero)")
        
    }
    
    
    lazy var titleLabel: Label = {
        let label = Label()
        label.textColor = UIColor.assist_000000
        label.font = Fonts.systemFont(ofSize: 16)
        label.text = "sleep_ratio".language
        return label
    }()
    
    
    public lazy var pieChartView = DFPieChartView.init(frame: .zero)
    

    lazy var deepLabel: Label = {
        let label = Label()
        label.textColor = UIColor.assist_222222
        label.font = Fonts.systemFont(ofSize: 12)
        return label
    }()
    
    lazy var shallowLabel: Label = {
        let label = Label()
        label.textColor = UIColor.assist_222222
        label.font = Fonts.systemFont(ofSize: 12)
        return label
    }()
    
    lazy var fastLabel: Label = {
        let label = Label()
        label.textColor = UIColor.assist_222222
        label.font = Fonts.systemFont(ofSize: 12)
        return label
    }()
    
    lazy var totalHourLabel: Label = {
        let label = Label()
        label.font = Fonts.boldSystemFont(ofSize: 13)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    lazy var totalMinuteLabel: Label = {
        let label = Label()
        label.font = Fonts.boldSystemFont(ofSize: 13)
        label.textColor = UIColor.assist_222222
        return label
    }()
    
    lazy var totalView:StackView = {
        let stackView = StackView()
        stackView.axis = .horizontal
        stackView.spacing = 4
        stackView.alignment = .bottom
        stackView.addArrangedSubview(totalHourLabel)
        
        let hourL = Label()
        hourL.textColor = UIColor.assist_222222
        hourL.font = Fonts.systemFont(ofSize: 10)
        hourL.text = "sleep_hour".language
        stackView.addArrangedSubview(hourL)
        
        stackView.addArrangedSubview(totalMinuteLabel)
        
        let minuteL = Label()
        minuteL.textColor = UIColor.assist_222222
        minuteL.font = Fonts.systemFont(ofSize: 10)
        minuteL.text = "sleep_min".language
        stackView.addArrangedSubview(minuteL)
        
        return stackView
    }()
    
    
}

extension SleepRatioCell {
    
   open class SleepSubRaTioView: View {
        
       public let hourLabel = Label()
        
       public let hourUnitLabel = Label()
        
       public let minLabel = Label()
        
       public let minUnitLabel = Label()
        
       public let icon = ImageView()
        
       public let titleLabel = Label()
        
       open override func makeUI() {
            super.makeUI()
           
           icon.layer.cornerRadius = 3.0
           hourLabel.textColor = UIColor.assist_222222
           hourLabel.font = Fonts.boldSystemFont(ofSize: 22.0)
           
           hourUnitLabel.textColor = UIColor.assist_222222
           hourUnitLabel.font = Fonts.systemFont(ofSize: 10)
           hourUnitLabel.text = "sleep_hour".language
           
           minLabel.textColor = UIColor.assist_222222
           minLabel.font = Fonts.boldSystemFont(ofSize: 22.0)
           
           minUnitLabel.textColor = UIColor.assist_222222
           minUnitLabel.font = Fonts.systemFont(ofSize: 10)
           minUnitLabel.text = "sleep_min".language
           
           titleLabel.textColor = UIColor.assist_A9A9A9
           titleLabel.font = Fonts.systemFont(ofSize: 12.0)
           
           let stackView = StackView()
           stackView.distribution = .equalSpacing
           stackView.axis = .horizontal
           stackView.alignment = .lastBaseline
           stackView.spacing = 2
           
           let stackView1 = StackView()
           stackView1.distribution = .equalCentering
           stackView1.axis = .horizontal
           stackView1.alignment = .center
           stackView1.spacing = 4
           
           stackView.addArrangedSubview(hourLabel)
           stackView.addArrangedSubview(hourUnitLabel)
           stackView.addArrangedSubview(minLabel)
           stackView.addArrangedSubview(minUnitLabel)
           addSubview(stackView)
           
           stackView1.addArrangedSubview(icon)
           stackView1.addArrangedSubview(titleLabel)
           addSubview(stackView1)
           
           
           icon.snp.makeConstraints { make in
               make.size.equalTo(6)
           }
           
           stackView.snp.makeConstraints { make in
               make.left.top.equalToSuperview()
               make.right.lessThanOrEqualToSuperview()
           }
           
           stackView1.snp.makeConstraints { make in
               make.left.equalTo(stackView.snp.left)
               make.top.equalTo(stackView.snp.bottom).offset(2)
               make.right.lessThanOrEqualToSuperview()
               make.bottom.equalToSuperview()
           }
           
           
           
           
            
        }
        
        
    }
    
    
}
